package cn.smartorigin.soas.frame;

import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.plaf.metal.MetalTextFieldUI;

public class CellTextField extends JTextField{

	private Border border = BorderFactory.createEmptyBorder(1, 3, 1, 3);
	private Font font = SoasUtil.FONT_12_PLAIN;
	
	 private Image backgroundLeftImage = new ImageIcon("img/login/textfield_background_left.png").getImage();
	 private Image backgroundRightImage =  new ImageIcon("img/login/textfield_background_right.png").getImage();
	 private ImageIcon backgroundImageIcon = new ImageIcon("img/login/textfield_background.png");
	 private TexturePaint paint = createTexturePaint(backgroundImageIcon.getImage());
	
    public CellTextField() {
        this(null);
    }

    public CellTextField(String text) {
        super(text);
        init();
    }
    
    private void init() {
        this.setBorder(border);
        this.setUI(new MetalTextFieldUI() {
            @Override
            protected void paintBackground(Graphics g) {
            	  Graphics2D g2d = (Graphics2D) g;
                  g2d.setPaint(paint);
                  g2d.fillRect(0, 0, getWidth(), getHeight());

                  //paint left side image.
                  g2d.drawImage(backgroundLeftImage, 0, 0, null);

                  //paint right side image.
                  g2d.drawImage(backgroundRightImage, getWidth() - backgroundRightImage.getWidth(null), 0, null);
            }
        });
        this.setFont(font);
    }
    
    private static TexturePaint createTexturePaint(Image image) {
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        BufferedImage bi = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = bi.createGraphics();
        g2d.drawImage(image, 0, 0, null);
        g2d.dispose();
        return new TexturePaint(bi, new Rectangle(0, 0, imageWidth, imageHeight));
    }
    
}
